<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包面试题</title>
</head>

<body>
    <script>
        function fun(n, o) {
            console.log(o)

            var obj = {
                fun: function (m) {
                    return fun(m, n)
                }
            }
            return obj
        }

        var a = fun(0) // ? undefined obj

        a.fun(1) // ?   obj.fun(1) = fun(1,0) = 0, obj
        a.fun(2) // ?   obj.fun(2) = fun(2,0) = 0, obj
        a.fun(3) // ?   obj.fun(3) = fun(3,0) = 0, obj

        var b = fun(0).fun(1).fun(2).fun(3) // ? undefined
        // obj.fun(1) = fun(1,0) = 0, obj.fun(2) = fun(2,1) = 1, obj.fun(3) = fun(3,2)
        // = 2, obj

        var c = fun(0).fun(1) // ? undefined  0, obj
        c.fun(2) // ?   1, obj
        c.fun(3) // ?   1, obj

        function foo(x, y) {
            console.log(y)
        }
    </script>
</body>

</html>